﻿using EasyXaf.EntityDesigner.BusinessObjects.Entities;
using EasyXaf.EntityDesigner.CodeTemplates.Extensions;
using System.Text;

namespace EasyXaf.EntityDesigner.CodeTemplates;

public class ExtensionEntityTemplate : TemplateBase
{
    public Entity Entity { get; }

    public ExtensionEntityTemplate(Entity entity)
    {
        Entity = entity;
    }

    public string GenerateCode()
    {
        var code = new StringBuilder();

        var entityNamespace = Entity.GetNamespace();
        if (!string.IsNullOrWhiteSpace(entityNamespace))
        {
            code.AppendLine(RenderTemplate("Namespace", new { Namespace = entityNamespace }));
            code.AppendLine();
        }

        code.AppendLine(RenderTemplate("TypeSummary", new
        {
            Summary = !string.IsNullOrWhiteSpace(Entity.Caption) ? Entity.Caption : Entity.Name,
            DescriptionLines = Entity.Description.ToLines()
        }));

        code.AppendLine(RenderTemplate("ClassStatement", new
        {
            ClassName = Entity.Name,
            Entity.IsAbstract
        }));

        code.AppendLine("{");
        code.AppendLine(RenderTemplate("ProtectedClassConstructor", new { ClassName = Entity.Name }));
        code.AppendLine("}");

        return code.ToString();
    }
}
